package com.zuipin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.product.ProductIntervalPrice;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IProductIntervalPriceService;
import com.zuipin.util.NumberIntervalUtil;
import com.zuipin.vo.ProductIntervalPriceVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 *
 * @ClassName ProductIntervalPriceController
 * @author wangcongyi
 * @date 2017年5月2日 下午4:41:25
 */
@Controller
@RequestMapping(value = "/productIntervalPrice")
@Api(value = "商品价格区间", description = "商品价格区间-wangcongyi")
public class ProductIntervalPriceController {
	
	@Resource
	private IProductIntervalPriceService productIntervalPriceService;
	
	@ResponseBody
	@RequestMapping(value = "/updateOrCreateProIntervalPrice", method = RequestMethod.POST)
	@ApiOperation(value = "新增或更新商品阶梯价(后台)", notes = "新增或更新商品阶梯价(后台)")
	@Valids({ @Valid(name = "price.productIntervalPrices.id", minValue = 1, maxValue = 999999999),
			@Valid(name = "price.productIntervalPrices.proExtId", required = true, minValue = 1, maxValue = 999999999),
			@Valid(name = "price.productIntervalPrices.beginNum", required = true, maxValue = 9999999),
			@Valid(name = "price.productIntervalPrices.endNum", required = true, maxValue = 9999999),
			@Valid(name = "price.productIntervalPrices.price", required = true, maxValue = 9999999) })
	public Result<?> updateOrCreateProIntervalPrice(@RequestBody @ApiParam ProductIntervalPriceVo price) {
		Result<Integer> result = new Result<>();
		// 判断集合是否为空
		List<ProductIntervalPrice> productIntervalPrices = price.getProductIntervalPrices();
		if (productIntervalPrices.size() > 0) {
			List<Integer> numbers = new ArrayList<>();
			for (int i = 0; i < productIntervalPrices.size(); i++) {
				numbers.add(productIntervalPrices.get(i).getBeginNum().intValue());
				numbers.add(productIntervalPrices.get(i).getEndNum().intValue());
			}
			boolean flag = NumberIntervalUtil.matches(numbers);
			if (!flag) {
				result.setErrorCode(ErrorCode.ELSE);
				result.setMessage("请检查区间是否完全闭合！");
				result.setData(null);
			} else {
				// 获取商品拓展id
				Long proExtId = productIntervalPrices.get(0).getProExtId();
				// 根据商品拓展id删除商品区间价格数据
				productIntervalPriceService.deleteByProExtId(proExtId);
				// 批量添加商品区间价格数据
				int count = productIntervalPriceService.insertBatch(productIntervalPrices);
				result.setErrorCode(ErrorCode.SUCCESS);
				result.setData(count);
			}
		} else {
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setData(null);
		}
		
		return result;
	}
}
